package cc.vicp.djx314.starfood.web.order;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cc.vicp.djx314.starfood.domain.account.User;
import cc.vicp.djx314.starfood.domain.order.Order;
import cc.vicp.djx314.starfood.domain.order.OrderDish;
import cc.vicp.djx314.starfood.service.order.OrderService;
import cc.vicp.djx314.starfood.util.DishTimeEnsure;
import cc.vicp.djx314.starfood.util.exception.StartProvideTimeLaterThanEndException;
import cc.vicp.djx314.starfood.web.exception.UserSessionTimeOutException;

@Controller
public class PlaceOrderController {
	public static final Logger logger = LoggerFactory.getLogger(PlaceOrderController.class);

	@Autowired
	DishTimeEnsure dishTimeEnsure;

	@Autowired
	OrderService orderService;

	@RequestMapping(value = "/user/order", method = RequestMethod.GET)
	public String userOrderPage(HttpSession session)
			throws StartProvideTimeLaterThanEndException, UserSessionTimeOutException {
		if (session.getAttribute("userOrder") == null) {
			User user = (User) session.getAttribute("loginUser");
			if (user == null) {
				throw new UserSessionTimeOutException("用户session过期");
			}
			Order order = new Order();
			List<OrderDish> orderDishes = new ArrayList<OrderDish>();
			order.setUser(user);
			order.setOrderDishes(orderDishes);
			session.setAttribute("userOrder", order);
		} else {
			Order order = (Order) session.getAttribute("userOrder");
			List<OrderDish> orderDishes = order.getOrderDishes();
			orderDishes = dishTimeEnsure.getSupportInfo(orderDishes, new Date());
			order.setOrderDishes(orderDishes);
		}
		return "customer/shopping/order";
	}

	@RequestMapping(value = "/user/order", method = RequestMethod.POST)
	public String userPlaceOrder(Model model, @ModelAttribute("placeOrder") @Valid Order order,
			BindingResult result, HttpSession session)
					throws Exception {
		String returnView = null;
		if (result.hasErrors()) {
			returnView = "customer/shopping/order";
		} else {
			List<OrderDish> orderDishes = order.getOrderDishes();
			orderDishes = orderService.getMoreOrderDishesInfo(orderDishes);
			orderDishes = dishTimeEnsure.getSupportInfo(orderDishes, new Date());
			boolean isDateSupport = true;
			for (OrderDish orderDish : orderDishes) {
				System.out.println(orderDish.getDish().getId());
				System.out.println(orderDish.getNowEnabled());
				if (orderDish.getNowEnabled() != true) {
					isDateSupport = false;
					break;
				}
			}

			if (isDateSupport == false) {
				returnView = "redirect:/user/order?timeout=true";
			} else {
				User user = (User) session.getAttribute("loginUser");
				if (user == null) {
					throw new UserSessionTimeOutException("用户session过期");
				}
				order.setUser(user);
				try {
					orderService.createOrder(order);
					session.removeAttribute("userOrder");
				} catch (Exception e) {
					logger.error("数据库发生未知错误", e);
					throw e;
				}
				returnView = "redirect:/user/ordersuccess";
			}
		}
		return returnView;
	}

	/*@RequestMapping(value = "/user/order1", method = RequestMethod.POST)
	public ModelAndView userPlaceOrder1(@ModelAttribute("placeOrder") Order order) {
		ModelAndView model = new ModelAndView("jsontournamenttemplate");
		return model;
	}*/

	@RequestMapping(value = "/user/ordersuccess", method = RequestMethod.GET)
	public String orderSuccess() {
		return "customer/shopping/ordersuccess";
	}
}